/* RgrServer.java
 * Brent Woodhouse
 * This file contains the code for the ringer server for the VOIP project.
 */

package project6.voip;
 
import java.io.*;
import java.net.*;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;

public class RgrServer
{	
	String user;
	
	public RgrServer(String user){
		this.user = user;
	}
	
	public void run() throws IOException//This code basically handles all the server side socket creation and giving the Connection Handler a socket to work with,
	{	
		ServerSocket servSock = null;
		try
		{
			System.out.println("Attempting to listen on 65534...");
			servSock = new ServerSocket(65534);
			System.out.println("Listening on 65534!");
		}
		catch(IOException e)
		{
			System.err.println("Could not listen on port: 65534");
			System.exit(1);
		}
		
		Socket clientSock = null;
		while(true)
		{
			try
			{
				System.out.println("Attempting to accept connections...");
				clientSock = servSock.accept();
				System.out.println("Connection Accepted!");
			}
			catch(IOException e)
			{
				System.err.println("Accept Failed.");
				System.exit(1);
			}
			
			Runnable rgrConnHandler = new RgrConnectionHandler(clientSock);
			new Thread(rgrConnHandler).start();
		}
	}
	
	public String getUser(){
		return user;
	}
}
